<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/view/common/tag.jsp" %>
<html>
<head>
    <title>房间信息添加</title>

    <link rel="stylesheet" href="${baseurl}/lib/layui/css/layui.css" media="all">

    <style>
        .form-custom {
            display: flex;
            justify-content: center;
            align-content: center;
            margin-top: 30px;
        }

        .layui-upload-img {
            width: 92px;
            height: 92px;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body>
<div class="form-custom">
    <form class="layui-form layui-form-pane" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <input type="hidden" name="id">
            <label class="layui-form-label">艺术馆名称</label>
            <div class="layui-input-block">
                <input type="text" name="galleryName" autocomplete="off" placeholder="请输入艺术馆名称" class="layui-input" lay-verify="required|galleryName" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">艺术馆地址</label>
            <div class="layui-input-block">
                <input type="text" name="galleryAddr" autocomplete="off" placeholder="请输入艺术馆地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="filePathElem">艺术馆图片</button>
                <input type="hidden" name="galleryImage" id="galleryImage" autocomplete="off" class="layui-input">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="filePath">
                    <p id="fileErrorText"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否开馆</label>
            <div class="layui-input-block">
                <input type="radio" name="isOpen" value="0" title="开启" checked>
                <input type="radio" name="isOpen" value="1" title="关闭">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接待人数</label>
            <div class="layui-input-block">
                <input type="text" name="receiveAmount" autocomplete="off" placeholder="请输入房间地址" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">建成时间</label>
            <div class="layui-input-block">
                <input type="text" name="buildDate" id="buildDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">艺术馆简介</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="gallerySummary"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <button class="layui-btn" lay-submit="" lay-filter="add">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
</div>

<script src="${baseurl}/lib/layui/layui.js" charset="utf-8"></script>
<script src="${baseurl}/lib/layui_custom/Hlayui.js" charset="utf-8"></script>
<script>
    var initData;

    function initForm(data) {
        var jsonString = JSON.stringify(data);
        initData = JSON.parse(jsonString);
    }
    layui.use(['form', 'jquery', 'upload', 'element', 'layer', 'laydate', 'layerCustom'], function () {
        var element = layui.element;
        $ = layui.jquery,
            upload = layui.upload,
            layer = layui.layer,
            layerCustom = layui.layerCustom,
            laydate = layui.laydate,
            form = layui.form;

        form.val("dataForm", initData);

        /*初始化图片地址*/
        $("#filePath").attr("src",initData.galleryImage);
        
        laydate.render({
            elem: '#buildDate'
            , format: 'yyyy-MM-dd'
        });

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#filePathElem'
            , url: BaseUrl + 'file/upload' //文件上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#filePath').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //设置到隐藏的 input中
                $("#galleryImage").val(res.data.url);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var fileErrorText = $('#fileErrorText');
                fileErrorText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                fileErrorText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        /*表单验证*/
        form.verify({

        });

        /*表单提交*/
        form.on('submit(add)', function (data) {
            var url = BaseUrl + 'galleryInfo/update';
            //保存接口
            $.post(url, data.field, function (result) {
                if (result.success) {
                    layer.msg(result.msg, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    })
                } else {
                    layer.msg(result.msg)
                    return;
                }
            }, 'json');

            return false;
        });
    });
</script>
</body>
</html>
